package com.dc.bobaoge.project.v2.to.v3.business.core.method.user;

import com.dc.bobaoge.project.base.core.old.entity.OldCoreUser;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreUserMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.business.system.account.mapper.SystemAccountMapper;
import com.own.business.system.user.entity.po.SystemUser;
import com.own.business.system.user.mapper.SystemUserMapper;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * UserUpdatePasswordResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-16 14:26:00
 */
@Slf4j
@Component
public class UserUpdateStateResolveMethod implements ResolveMethod {

    @Resource
    private OldCoreUserMapper oldCoreUserMapper;

    @Resource
    private SystemUserMapper systemUserMapper;
    @Resource
    private SystemAccountMapper systemAccountMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        // 查询用户信息是否存在
        var oldCoreUser = oldCoreUserMapper.selectById(trigger.getTargetId());
        if (oldCoreUser == null) {
            throw new BusinessSimpleException("用户信息不存在");
        }
        // 查询是否已经存在当前用户了
        var systemUser = systemUserMapper.selectById(oldCoreUser.getUserId());
        if (systemUser == null) {
            throw new BusinessSimpleException("用户信息不存在");
        }
        buildUserState(systemUser, oldCoreUser);
        systemUserMapper.updateById(systemUser);
    }

    /**
     * 修改用户的状态信息
     *
     * @param user        用户信息
     * @param oldCoreUser 旧数据库用户数据
     * @return 用户信息
     */
    public SystemUser buildUserState(SystemUser user, OldCoreUser oldCoreUser) {
        // 用户状态
        if (oldCoreUser.getUserState() == 0) {
            user.setState(2);
        } else if (oldCoreUser.getUserState() == 1) {
            user.setState(1);
            user.setIsDisable(0);
        } else if (oldCoreUser.getUserState() == 2) {
            user.setIsDisable(1);
        } else {
            user.setState(1);
        }
        return user;
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "user_state";
    }

}
